package model.datasource;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// gets data source based on config file
public class DataSourceFactory
{
	private static Logger logger = LoggerFactory.getLogger(DataSourceFactory.class);
	
	private static DataSource source = null;
	public static DataSource getDataSource()
	{
		if(source == null)
		{		
			try
			{
				logger.info("creating datasource...");
				
				Properties props = new Properties();
				
				props.load(ClassLoader.getSystemResourceAsStream("config.properties"));
				if(props.getProperty("file") != null)
				{
					String fileName = props.getProperty("file");
					logger.info("creating filedatasource " + fileName);
					source = new FilePlainTextDataSource(fileName);
				}
				else if(props.getProperty("database") != null)
				{
					logger.info("database is used");
					DatabaseManager.getInstance().initialize();
					source = new DatabaseDataSource();
				}
				else
				{
					logger.error("error no source specified!");
				}
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		return source;
	}
}
